package com.example.easypoi.controller;

import com.example.easypoi.mapper.ProjectsMapper;
import com.example.easypoi.mapper.ResumeLibMapper;
import com.example.easypoi.mapper.UserMapper;
import com.example.easypoi.mapper.WorkMapper;
import com.example.easypoi.model.DomeVo;
import com.example.easypoi.model.ProjectsVo;
import com.example.easypoi.model.ResumeVo;
import com.example.easypoi.model.WorkVo;
import com.example.easypoi.utils.ExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * excel 导出控制器
 *
 * @author novel
 * @date 2018/12/27
 */
@RestController
@RequestMapping("excel")
public class ExcelController5 {

    private final static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


    @Autowired
    private ResumeLibMapper resumeLibMapper;
    @Autowired
    private WorkMapper workMapper;
    @Autowired
    private ProjectsMapper projectsMapper;
    @Autowired
    private UserMapper userMapper;


    @RequestMapping(value = "/import5", method = RequestMethod.GET)
    public void importExcel(HttpServletResponse response) throws IOException {
        String filePath = "E:\\新建文件夹\\WeChat Files\\wxid_zhxstnivcpt022\\FileStorage\\File\\2020-10\\5.xlsx";
        List<DomeVo> domeVos = ExcelUtils.importExcel(filePath, 1, 1, DomeVo.class);
        List<ResumeVo> resumeVos = new ArrayList<>();
        int i = 0;
        for(DomeVo domeVo : domeVos){
            i++;
            String id = domeVo.getId();
            ResumeVo resumeVo = resumeLibMapper.getById(id);
            if(null != resumeVo){
                if(null == resumeVo.getSex()){
                    resumeVo.setSex("-");
                }
                if(null == resumeVo.getAge()){
                    resumeVo.setAge("-");
                }
                if(null == resumeVo.getCity()){
                    resumeVo.setCity("-");
                }
                if(null == resumeVo.getZn()){
                    resumeVo.setZn("-");
                }
                if(null == resumeVo.getCreatedBy()){
                    resumeVo.setCreatedBy(userMapper.getById(resumeVo.getUserId()).getName());
                }
                if(null == resumeVo.getGllueId()){
                    resumeVo.setGllueId("-");
                }
                if(null == resumeVo.getHy()){
                    resumeVo.setHy("-");
                }
                if(null == resumeVo.getUpdateBy()){
                    resumeVo.setUpdateBy("-");
                }
                if(null != resumeVo.getCreated()){
                    resumeVo.setCreatedStr(sdf.format(resumeVo.getCreated()));
                }
                if(null != resumeVo.getUpdated()){
                    String str = "-";
                    try{
                        str = sdf.format(resumeVo.getUpdated());
                    } catch (Exception e){
                        e.getMessage();
                        System.out.println(id);
                    }
                    resumeVo.setUpdatedStr(str);
                }else {
                    resumeVo.setUpdatedStr("-");
                }
                List<WorkVo> workVos = workMapper.getByResumeId(Long.parseLong(id));
                for(int k = 1; k <= workVos.size(); k++){
                    WorkVo workVo = workVos.get(k-1);
                    if(null != workVo){
                        if(k == 1){
                            if(null != workVo.getCompanyName()){
                                resumeVo.setWorkCompanyName(workVo.getCompanyName());
                            }
                            if(null != workVo.getJobTitle()){
                                resumeVo.setWorkJobTitle(workVo.getJobTitle());
                            }
                            if(null != workVo.getDescription()){
                                resumeVo.setWorkDescription(workVo.getDescription());
                            }
                        }
                        if(k == 2){
                            if(null != workVo.getCompanyName()){
                                resumeVo.setWorkCompanyName2(workVo.getCompanyName());
                            }
                            if(null != workVo.getJobTitle()){
                                resumeVo.setWorkJobTitle2(workVo.getJobTitle());
                            }
                            if(null != workVo.getDescription()){
                                resumeVo.setWorkDescription2(workVo.getDescription());
                            }
                        }
                        if(k == 3){
                            if(null != workVo.getCompanyName()){
                                resumeVo.setWorkCompanyName3(workVo.getCompanyName());
                            }
                            if(null != workVo.getJobTitle()){
                                resumeVo.setWorkJobTitle3(workVo.getJobTitle());
                            }
                            if(null != workVo.getDescription()){
                                resumeVo.setWorkDescription3(workVo.getDescription());
                            }
                        }
                    }
                }
                List<ProjectsVo> projectsVos = projectsMapper.getByResumeId(Long.parseLong(id));
                for(int j = 1; j <= projectsVos.size(); j++){
                    ProjectsVo projectsVo = projectsVos.get(j - 1);
                    if(null != projectsVo){
                        if(j == 1){
                            if(null != projectsVo.getProjectName()){
                                resumeVo.setProjectName(projectsVo.getProjectName());
                            }
                            if(null != projectsVo.getCompanyName()){
                                resumeVo.setCompanyName(projectsVo.getCompanyName());
                            }
                            if(null != projectsVo.getJobTitle()){
                                resumeVo.setJobTitle(projectsVo.getJobTitle());
                            }
                            if(null != projectsVo.getJobFunction()){
                                resumeVo.setJobFunction(projectsVo.getJobFunction());
                            }
                            if(null != projectsVo.getDescription()){
                                resumeVo.setDescription(projectsVo.getDescription());
                            }
                            if(null != projectsVo.getAchievement()){
                                resumeVo.setAchievement(projectsVo.getAchievement());
                            }
                            if(null != projectsVo.getInfo()){
                                resumeVo.setInfo(projectsVo.getInfo());
                            }
                        }
                        if(j == 2){
                            if(null != projectsVo.getProjectName()){
                                resumeVo.setProjectName2(projectsVo.getProjectName());
                            }
                            if(null != projectsVo.getCompanyName()){
                                resumeVo.setCompanyName2(projectsVo.getCompanyName());
                            }
                            if(null != projectsVo.getJobTitle()){
                                resumeVo.setJobTitle2(projectsVo.getJobTitle());
                            }
                            if(null != projectsVo.getJobFunction()){
                                resumeVo.setJobFunction2(projectsVo.getJobFunction());
                            }
                            if(null != projectsVo.getDescription()){
                                resumeVo.setDescription2(projectsVo.getDescription());
                            }
                            if(null != projectsVo.getAchievement()){
                                resumeVo.setAchievement2(projectsVo.getAchievement());
                            }
                            if(null != projectsVo.getInfo()){
                                resumeVo.setInfo2(projectsVo.getInfo());
                            }
                        }
                        if(j == 3){
                            if(null != projectsVo.getProjectName()){
                                resumeVo.setProjectName3(projectsVo.getProjectName());
                            }
                            if(null != projectsVo.getCompanyName()){
                                resumeVo.setCompanyName3(projectsVo.getCompanyName());
                            }
                            if(null != projectsVo.getJobTitle()){
                                resumeVo.setJobTitle3(projectsVo.getJobTitle());
                            }
                            if(null != projectsVo.getJobFunction()){
                                resumeVo.setJobFunction3(projectsVo.getJobFunction());
                            }
                            if(null != projectsVo.getDescription()){
                                resumeVo.setDescription3(projectsVo.getDescription());
                            }
                            if(null != projectsVo.getAchievement()){
                                resumeVo.setAchievement3(projectsVo.getAchievement());
                            }
                            if(null != projectsVo.getInfo()){
                                resumeVo.setInfo3(projectsVo.getInfo());
                            }
                        }
                    }
                }
                resumeVos.add(resumeVo);
            }
            System.out.println("运行了第" + i + "行");
        }
        ExcelUtils.exportExcel(resumeVos, null, "人才列表5", ResumeVo.class, "人才列表5", response);
    }
}
